#include "utils.h"

int anselmu_running;
SDL_Surface *screen = NULL;

/* Fonts and sounds */
TTF_Font *oneDirection = NULL;
Mix_Chunk *ball_hit_snd = NULL, *ball_respawn_snd = NULL;

/* Credits */
char *credits_array[] =
{
  "GUEJO EXO POR XERPI",
  "",
  "GLASIAS A TODOS POR JUJAR",
  NULL
};


void initSDL()
{
    srand(time(NULL));
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
        return 1;
    }
    atexit(SDL_Quit);

    screen = SDL_SetVideoMode(SCREEN_W, SCREEN_H, 32,
                              SDL_HWSURFACE|SDL_DOUBLEBUF);
    if (!screen )
    {
        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
        return 1;
    }

    int audio_rate = 22050;
    Uint16 audio_format = AUDIO_S16SYS;
    int audio_channels = 2;
    int audio_buffers = 4096;

    if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0)
    {
        printf("Unable to initialize audio: %s\n", Mix_GetError());
        exit(1);
    }
    if (TTF_Init() == -1)
    {
        printf("Unable to initialize SDL_ttf: %s \n", TTF_GetError());
    }

    loadSounds();
    loadFonts();
    anselmu_running = 1;
}

void closeSDL()
{
    Mix_CloseAudio();
    cleanSounds();
    cleanFonts();
    TTF_Quit();
    SDL_Quit();
}

void drawRect(int x, int y, int w, int h, unsigned int color)
{
    SDL_Rect rect = {x, y, w, h};
    SDL_FillRect(screen, &rect, color);
}


void anselmuEvents()
{
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {
        switch (event.type)
        {
            case SDL_QUIT:
                anselmu_running = 0;
                break;
            case SDL_KEYDOWN:
            {
                if (event.key.keysym.sym == SDLK_ESCAPE)
                {
                    anselmu_running = 0;
                }
                if (event.key.keysym.sym == SDLK_r)
                {
                    resetBall(&ball);
                }
                else if(event.key.keysym.sym == SDLK_UP)
                {
                     moveRacket(&racket1, -1);
                }
                else if(event.key.keysym.sym == SDLK_c)
                {
                     credits();
                }
                break;
            }
            default:
                break;
        }
    }

    Uint8 *keystate = SDL_GetKeyState(NULL);
    if ( keystate[SDLK_DOWN] ) moveRacket(&racket1, 1);
}


void loadFonts()
{
    oneDirection = TTF_OpenFont("data/font/OneDirection.ttf", 25);
}

void cleanFonts()
{
    TTF_CloseFont(oneDirection);
}

void loadSounds()
{
    ball_hit_snd = Mix_LoadWAV("data/sound/ball_hit.wav");
    ball_respawn_snd = Mix_LoadWAV("data/sound/ball_respawn.wav");
}

void cleanSounds()
{
    Mix_FreeChunk(ball_hit_snd);
    Mix_FreeChunk(ball_respawn_snd);
}


void credits()
{
    int i = -20, j = 0;
    SDL_Surface *font;
    SDL_Rect rect = {SCREEN_W/2-120, i,0,0};
    for(; i < SCREEN_H + 20; i++, j = 0)
    {
        clearScreen();
        do{
            font = TTF_RenderText_Solid(oneDirection, credits_array[j], (SDL_Color){255,0,255});
            rect.y = i + j * 20;
            SDL_BlitSurface(font, NULL, screen, &rect);
            SDL_FreeSurface(font);

        }while(credits_array[++j]);
        SDL_Flip(screen);
    }
}
